package com.shixi.shixitraining.shixitraining.ui.tabmine.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.ui.tabmine.entity.RegisterBean;
import com.shixi.shixitraining.shixitraining.util.Base64Utils;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {


    private EditText mUserEdt;
    private EditText mEamilEdt;
    private EditText mPswEdt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }
    private void initView() {

        ImageView mClose = (ImageView) findViewById(R.id.register_close);
        TextView btnRegister = (TextView) findViewById(R.id.btn_register);
        mClose.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
        mUserEdt = (EditText) findViewById(R.id.edt_user);
        mEamilEdt = (EditText) findViewById(R.id.edt_eamil);
        mPswEdt = (EditText) findViewById(R.id.edt_psw);


    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register_close:
                killActivity();
                break;
            case R.id.btn_register:
                userRegister();
                break;
        }
    }

    private void userRegister() {
        String     mUser = mUserEdt.getText().toString();
        String     mEmail = mEamilEdt.getText().toString();
        String     mPsw = mPswEdt.getText().toString();
        String s = Base64Utils.getBase64(mPsw);
        Log.e("tag",mUser);
        if (TextUtils.isEmpty(mUser)){
            ToastUtil.showShortToast(this,"用户名不能为空");
            return;
        }else if (TextUtils.isEmpty(mEmail)){
            ToastUtil.showShortToast(this,"邮箱不能为空");
            return ;
        }else if (TextUtils.isEmpty(mPsw)){
            ToastUtil.showShortToast(this,"密码不能为空");
            return ;
        }
        requestRegister(mUser,mEmail,s);

    }

    private void requestRegister(String mUser, String mEmail, String s) {

        Map<String,String> map=new HashMap<>();
        map.put("name",mUser);
        map.put("email",mEmail);
        map.put("password",s);
        HttpUtil.getInstance().getApiInterface().register(map).enqueue(new CallbackImple<RegisterBean>() {
            @Override
            public void onError(Call<RegisterBean> call, Throwable t) {
                ToastUtil.showShortToast(RegisterActivity.this,t.getMessage());
            }

            @Override
            public void onSuccess(Call<RegisterBean> call, RegisterBean response) {
                if (response!=null){
                    if (response.getStatus()==200){
                        finish();
                    }else {
                        ToastUtil.showShortToast(RegisterActivity.this,response.getMsg());
                    }
                }else {
                    ToastUtil.showShortToast(RegisterActivity.this,"数据不正确");
                }

            }
        });




    }


    private void killActivity() {
        this.finish();
        //必须在finish后调用
        this.overridePendingTransition(R.anim.activity_stay,R.anim.activity_out);
    }
}
